<?php
class detail extends ujn_controller {
	function index() {
		
		$id = $this->get('id', 0);
		
		$siteid = $this->get('siteid', 1);
		$do = $this->get('do', '');
		
		$this->assign('id', $id);
		$page = $this->get('page', 1);
		
		$data = mdl_news::get_instance()->get($id);
		$datas = mdl_category::get_instance()->get_sub($siteid, array());
		
		$curr_cat = array();
		if(isset($datas[$data['info']['catid']])) {
			$curr_cat = $datas[$data['info']['catid']];
			$arrparentid = explode(',', $curr_cat['arrparentid']);
			$data['pid'] = $arrparentid[1] ? $arrparentid[1] : $data['info']['catid'];
			$data['p_path'] = array();
			foreach($arrparentid as $v) {
				if(!empty($v))
					$data['p_path'][] = $datas[$v];
			}
			$data['p_path'][] = $curr_cat;
		}
		
		if(isset($data['data']['copyfrom'])) { // 转换来源信息
			$data['data']['copyfrom'] = explode('|', $data['data']['copyfrom']);
			if(!isset($data['data']['copyfrom'][1]) || $data['data']['copyfrom'][1] == 0) {
				if(substr($data['data']['copyfrom'][0], 0, 1) == '<') {
					$data['data']['copyfrom'] = $data['data']['copyfrom'][0];
				} else {
					$data['data']['copyfrom'] = '<a href="http://www.kekenet.com/">' . $data['data']['copyfrom'][0] . '</a>';
				}
			} else {
				$d = mdl_category::get_instance()->get_copyfrom_byid($data['data']['copyfrom'][1]);
				$data['data']['copyfrom'] = '<a href="' . $d['siteurl'] . '">' . $d['sitename'] . '</a>';
			}
		}
		
		$data['data']['content'] = str_replace('&nbsp;', ' ',isset($data['data']['content']) ? $data['data']['content'] : '');
		//$data['data']['content'] = stripslashes($data['data']['content']);

		$c = $data['data']['content'];
		$c = explode('[page]', $c);
		$cons = array();
		$tips = array();
		foreach($c as $k => $f) {
			$f = explode('[/page]', $f);
			$_t = '';
			if(count($f) == 1) { // 单个 [page模式]
			} else { // 带标题模式
				$_t = $f[0];
				$f[0] = $f[1];
			}
			$x = str_replace(array("\r\n","\t",'[page]','[/page]','[qh]','&ldquo;','&rdquo;','&nbsp;'), '', strip_tags($f[0]));
			if(!empty($x)) {
				$cons[] = $f[0];
				if(!empty($_t)) {
					$tips[] = array(
							'p' => count($cons),
							't' => $_t
					);
				}
			}
		}
		
		if(empty($cons[$page-1])) die('s');
		
		$data['data']['content'] = $cons[$page-1];

		// $data['info']['url'] = 'sSdf/sdf/123123.shtml';
		$data['info']['page'] = $page;
		$data['info']['url'] = str_replace('.shtml', '', $data['info']['url'] ); // 为方便前台显示, 这里干掉.shtml的部分, 前台需要加上
		$data['max_page'] = count($cons);
		
		if($data['data']['isnot'] == '1') {
			if($page==1){
				$content = str_replace(array('<br>','<br />','<br/>'),array('','',''),$data['data']['content']);
				$content = preg_replace('/\[qh]\s*/','[qh]',$content); //去除[qh]后面的空格
				$content = str_replace( "[qh]</p>", "</p>[qh]",$content);
				$contlist = explode('[qh]',$content);
				$txlist = array();
				foreach($contlist as $k => $v){
					if($v){
						if($k%2==0){
							$i++;
						}
						$txlist[$k]['k']= $k+1;
						$txlist[$k]['id']= $i;
						$txlist[$k]['tx'] = $v;
					}
				}
				$this->assign('txlist', $txlist);
			}
		}else if($data['data']['isnot'] == '2') {
			if($page == 1){
				$content = preg_replace('/\[qh]\s*/','[qh]',$data['data']['content']); //去除[qh]后面的空格
				$content = str_replace( "[qh]</p>", "</p>[qh]",$content);
				$zylist = explode('[qh]',$content);
				$this->assign('zylist', $zylist);
			}
		}else{
			if($page - 1 > 2){
				$strs = $data['info']['url'] . '_' . ($page-1) . '.shtml';
			}else{
				$strs = $data['info']['url'].'.shtml';
			}

			if($page + 1 < $data['max_page']){
				$strx = $data['info']['url'] . '_' . ($page + 1) . '.shtml';
			}else{
				$strx = $data['info']['url'] . '_' . $data['max_page'] . '.shtml';
			}
			$this->assign('strs', $strs);
			$this->assign('strx', $strx);

			$data['data']['content'] = str_replace("<img","<img onmouseover='upNext(this)' ",$data['data']['content']);
		}
		
		$data['page'] = $page;
		$data['tips'] = $tips;
		$data['curl'] = $curr_cat['url'];
		
		$this->assign('data', $data);
		// pages
		
		$this->assign('base_url', ujn::get_cfg('url.home'));
		
		switch($do) {
			case 'makestatic':
				$this->set_view_path('detail/show.html');
				
				// make!
				$out_file = mdl_news::get_instance()->make_static_path($data, $curr_cat);
				if($page > 1) {
					list($out_file) = explode('.shtml', $out_file);
					$out_file .= "_{$page}.shtml";
				}
				ujn::bind_cfg(array('output_static_html' => $out_file));
				
				$ppids = array();
				foreach($data['p_path'] as $p) {
					$ppids[] = $p['catid'];
				}
				$pages = array(); // 子分页
				for($i=2;$i<=$data['max_page'];$i++) {
					$pages[] = $i;
				}
				ujn::bind_cfg(array('output_params' => array(
						'ppids'=>$ppids,
						'pages'=>$pages,
						'url' => $out_file
						)));
				//判断是否有lrc文件
// 				$ourl = @fopen('http://www.kekenet.com/'.$data['data']['fileurl'].'.lrc','r');
// 				$this->assign('ourl', $ourl); 
				
// 				//判断是否有pdf文件
// 				$pdfl = @fopen('http://x.kekenet.com/qita/pdf/kekenet_'.$id.'.pdf','r');
// 				$this->assign('pdfl', $pdfl);

// 				//判断是否有xml文件
// 				$xmll = @fopen('http://x.kekenet.com/qita/xml/'.$id.'.xml','r');
// 				$this->assign('xmll', $xmll);
				
				$cats = mdl_category::get_instance()->get($data['pid']);
				$catids = $cats['arrchildid'];
				// 当前频道的本月的排行数据
				$x = mdl_category::get_instance()->find_list_mon($catids, $siteid, 8);
				foreach($x as $k => $d){
					$x[$k]['k'] = $k+1;
				}
				$this->assign('mon_list', $x);
				
				// 全站的排行数据
				/*$x = mdl_category::get_instance()->find_list_all('', $siteid);
				foreach($x as $k => $d){
					$x[$k]['k'] = $k+1;
				}
				$this->assign('all_list', $x); */

				//tags标签
				$tags = mdl_news::get_instance()->get_tags($id,$siteid);
				$this->assign('tags', $tags); 

				//感兴趣的文章列表
				$interests = mdl_news::get_instance()->get_interest($id, $data['info']['catid']);
				$this->assign('interests', $interests); 
				
				break;
			case 'preview':
			default:

				$this->set_view_path('detail/show.html');
				
				//判断是否有lrc文件
				$ourl = @fopen('http://www.kekenet.com/'.$data['data']['fileurl'].'.lrc','r');
				$this->assign('ourl', $ourl); 
				
				//判断是否有pdf文件
				$pdfl = @fopen('http://x.kekenet.com/qita/pdf/kekenet_'.$id.'.pdf','r');
				$this->assign('pdfl', $pdfl);

				//判断是否有xml文件
				$xmll = @fopen('http://x.kekenet.com/qita/xml/'.$id.'.xml','r');
				$this->assign('xmll', $xmll);
				
				$cats = $datas[$data['pid']]; // mdl_category::get_instance()->get($data['pid']);
				$catids = $cats['arrchildid'];
				// 当前频道的本月的排行数据
				$x = mdl_category::get_instance()->find_list_mon($catids, $siteid, 8);
				foreach($x as $k => $d){
					$x[$k]['k'] = $k+1;
				}
				$this->assign('mon_list', $x);
				
				// 全站的排行数据
				/*$x = mdl_category::get_instance()->find_list_all('', $siteid);
				foreach($x as $k => $d){
					$x[$k]['k'] = $k+1;
				}
				$this->assign('all_list', $x); */

				//tags标签
				$tags = mdl_news::get_instance()->get_tags($id,$siteid);
				$this->assign('tags', $tags); 

				//感兴趣的文章列表
				$interests = mdl_news::get_instance()->get_interest($id, $data['info']['catid']);
				$this->assign('interests', $interests);

				break;
		}
	}
	
	function add() { // 添加
		
		$siteid = $this->get('siteid', 1);
		$pid = $this->get('pid', 0);
		
		if($this->method == 'POST') {
			
			/**
			 * 请在这里增加对$_POST的细节处理操作
			 */
			$d = mdl_category::get_instance()->get($_POST['catid']);
			list($empty, $empty, $cid) = explode(',', $d['arrparentid']);
			
			$new_id = mdl_news::get_instance()->add($_POST, $siteid);
			// echo ( !empty($new_id) ? '添加成功' : '添加失败' ). ', 3s自动返回';
			// tls_func::redirect("./index.php/category.index?pid={$pid}", 'html', 3);
			
			$this->assign(array(
					'id' => $new_id,
					'pid' => $pid,
					'cid' => $this->get('cid', $cid),
					'catid' => $_POST['catid'],
					'url' => '',
					'next_url' => "../index.php/category.index?pid={$pid}",
					'gtitle' => $_POST['title'],
					));
			
			$this->set_view_path('detail/success.html');
		} else {
			$id = $this->get('id', 0);
			$this->_make_info($siteid, $pid, array());
			
			$curr = mdl_account::get_instance()->get_current();
			
			$this->assign('catid', $this->get('catid', 0));
			if(!empty($id)) {
				$curr = mdl_news::get_instance()->get($id);
				$out = $curr['info'] + $curr['data'] + $curr['kkq'];
				$out['keywords'] = implode(',', explode(' ', $out['keywords']));
				
				if(isset($out['copyfrom']) && substr($out['copyfrom'], 0, 1) != '|') {
					$out['copyfrom'] = explode('|', $out['copyfrom']);
					$out['copyfrom'] = $out['copyfrom'][0];
					$dom = new domDocument;
				
					$dom->loadHTML('<meta http-equiv="Content-Type" content="text/html;charset=utf8">' . $out['copyfrom']);
					$dom->preserveWhiteSpace = false;
				
					$tables = $dom->getElementsByTagName('a')->item(0);
				
					$out['copyfrom_href'] = $tables->getAttribute('href');
					$out['copyfrom_text'] = $tables->textContent;
				}
				
				$out['id'] = 0;
				$out['now_name'] = $out['author'];
				$out['now_time'] = date('Y-m-d H:i:s', $out['inputtime']);
				$this->assign($out);
			} else {
				$this->assign('id', 0);
				$this->assign('islink', 0);
				
				$this->assign('copyfrom', '');
				$this->assign('paginationtype', '');
				$this->assign('posids', '');
				$this->assign('isnot', '');
				$this->assign('attribute', array());
				
				$this->assign('now_name', $curr['uname_nick']);
				$this->assign('now_time', date('Y-m-d H:i:s'));
			}
		}
	}
	
	function edit() { // 编辑
		
		$siteid = $this->get('siteid', 1);
		$id = $this->get('id', 0);
		$pid = $this->get('pid', 0);
		$this->assign('pid', $pid);
		
		if($this->method == 'POST') {
		
			/**
			 * 请在这里增加对$_POST的细节处理操作
			 */
			
			$new_id = mdl_news::get_instance()->modify($_POST, $siteid, $id);
			$this->assign(array(
					'id' => $new_id,
					'cid' => $this->get('cid', 0),
					'catid' => $_POST['catid'],
					'next_url' => "../index.php/category.index?pid={$pid}",
					'url' => '',
					'gtitle' => $_POST['title'],
					));
			$this->set_view_path('detail/success.html');
		} else {
			$curr = mdl_news::get_instance()->get($id);
			$out = $curr['info'] + $curr['data'] + $curr['kkq'];
			$out['keywords'] = isset($out['keywords']) ? implode(',', explode(' ', $out['keywords'])) : '';
			$_a = array();
			if(!isset($out['attribute'])) $out['attribute'] = '';
			foreach(explode(',', $out['attribute']) as $d) {
				$_a[$d] = '1';
			}
			$out['attribute'] = $_a;

			$out['now_name'] = isset($out['author']) ? $out['author'] : '';
			$out['now_time'] = isset($out['inputtime']) ? date('Y-m-d H:i:s', $out['inputtime']) : date('Y-m-d H:i:s');
			
			if(isset($out['copyfrom']) && substr($out['copyfrom'], 0, 1) != '|') {
				$out['copyfrom'] = explode('|', $out['copyfrom']);
				$out['copyfrom'] = $out['copyfrom'][0];
				$dom = new domDocument;
				
				$dom->loadHTML('<meta http-equiv="Content-Type" content="text/html;charset=utf8">' . $out['copyfrom']);
				$dom->preserveWhiteSpace = false;
				
				$tables = $dom->getElementsByTagName('a')->item(0);
				
				$out['copyfrom_href'] = $tables != null ? $tables->getAttribute('href') : 'http://www.kekenet.com';
				$out['copyfrom_text'] = $tables != null ? $tables->textContent : $out['copyfrom'];
			}
			
			$this->assign($out);
			
			$this->_make_info($siteid, $pid, $out);
			$this->set_view_path('detail/add.html');
		}
	}
	
	function del() { // 删除
		
		$siteid = $this->get('siteid', 1);
		$id = $this->get('id', 0);
		$pid = $this->get('pid', 0);
		
		mdl_news::get_instance()->del($id);
		echo '删除成功, 3s自动跳回';
		tls_func::redirect('', 'html', 3);
	}
	
	function make_all() {
		
		$siteid = 1;
		
		$datas = mdl_category::get_instance()->get_sub($siteid, array());
		$sub_cats = array();
		foreach($datas as $k => $d) { // 最顶级
			if($d['parentid'] == 0) {
				$sub_cats[] = $d;
			}
		}
		
		foreach($sub_cats as $key => $d) {
			$caid = explode(',', $d['arrchildid']);
			$sub_cats[$key]['child'] = array();
			foreach($caid as $k => $v) {
				$datas[$v]['child'] = array();
				$_caid = explode(',', $datas[$v]['arrchildid']);
				foreach($_caid as $kk => $dd) {
					if($dd != $v && isset($datas[$dd])) {
						$datas[$v]['child'][] = $datas[$dd];
					}
				}
				if($sub_cats[$key]['catid'] == $datas[$v]['parentid'])
					$sub_cats[$key]['child'][] = $datas[$v];
			}
		}
		
		$this->assign('sub_cats', $sub_cats);
	}
	
	private function _make_info($siteid, $pid, $data = array()) {
		
		$privs = mdl_category::get_instance()->get_priv($siteid, 'add');
		$datas = mdl_category::get_instance()->get_sub($siteid, $privs);
		$sub_cats = array();
		foreach($datas as $k => $d) { // 最顶级
			if(!empty($pid) && $d['parentid'] == $pid) {
				$sub_cats[] = $d;
			}
		}
		
		foreach($sub_cats as $key => $d) {
			$caid = explode(',', $d['arrchildid']);
			$sub_cats[$key]['child'] = array();
			foreach($caid as $k => $v) { // 权限过滤
				if(!empty($privs) && (!isset($privs[$v]) || $v == $d['catid']) ) unset($caid[$k]);
				else {
					$datas[$v]['child'] = array();
					$_caid = explode(',', $datas[$v]['arrchildid']);
					foreach($_caid as $kk => $dd) {
						if($dd != $v && isset($datas[$dd])) {
							$datas[$v]['child'][] = $datas[$dd];
						}
					}
					if($sub_cats[$key]['catid'] == $datas[$v]['parentid'])
						$sub_cats[$key]['child'][] = $datas[$v];
				}
			}
		}
// 		var_dump($sub_cats); exit;
		$this->assign('sub_cats', $sub_cats);
		$this->assign('cf_list', mdl_category::get_instance()->get_copyfrom($siteid));
	}
}